package _15_generics._15_2._15_2_2;

/**
 * 自己实现内部存储机制
 * @param <T>
 */
public class LinkedStack<T> {
    private static class Node<U>{
        U item;
        Node<U> next;
        Node(){
            this.item = null;
            this.next = null;
        }
        Node(U item,Node<U> next){
            this.item = item;
            this.next = next;
        }
        boolean end(){
            return this.item == null && next == null;
        }
    }
    private Node<T> top = new Node<T>();
    public void push(T item){
        top = new Node<T>(item,top);
    }
    public T pop(){
        T item = top.item;
        if (!top.end()){
            top = top.next;
        }
        return item;
    }

    public static void main(String[] args) {
        LinkedStack<String> stack = new LinkedStack<String>();
        stack.push("hello");
        stack.push(",");
        stack.push("world");
        String pop = null;
        while ((pop = stack.pop()) != null){
            System.out.println(pop);
        }
    }
}
